- /* sffffsub.cpp by K.Tsuru */
- // function ID = 711 DARDIX
- /*********************************
- SFraction class
- It provides the subtraction m-n.
- See "sfract.h" about the reduction.
- **********************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
-
- SFraction FFSub(const SFraction& x, const SFraction& y){
- SFraction z;
- #if REDUCE_SIZE==0 //use Knuth's method
- SLong d1, d2, t, xd1;
- d1 = gcdL(x.den, y.den);
- if(d1.IsOne()) { // d1 == 1
- z.den = x.den*y.den;
- z.num = x.num*y.den - y.num*x.den;
- } else {
- xd1 = x.den/d1;
- t = x.num*(y.den/d1) - y.num*xd1;
- d2 = gcdL(t, d1);
- if(d2.IsOne()){
- z.den = xd1*y.den;
- z.num = t;
- } else {
- z.den = xd1*(y.den/d2);
- z.num = t/d2;
- }
- }
- if( !z.num.Sign(701) ) z.den.SetShort(1); //It becomes zero.
- z.reduceDone = true;
- return z;
- #else
- if(x.ReduceStepByStep()) {
- SLong d1, d2, t, xd1;
- d1 = gcdL(x.den, y.den);
- if(d1.IsOne()) { // d1 == 1
- z.den = x.den*y.den;
- z.num = x.num*y.den - y.num*x.den;
- } else {
- xd1 = x.den/d1;
- t = x.num*(y.den/d1) - y.num*xd1;
- d2 = gcdL(t, d1);
- if(d2.IsOne()){
- z.den = xd1*y.den;
- z.num = t;
- } else {
- z.den = xd1*(y.den/d2);
- z.num = t/d2;
- }
- }
- if( !z.num.Sign(701) ) z.den.SetShort(1); //It becomes zero.
- z.reduceDone = true;
- return z;
- } else {
- z.den = x.den*y.den;
- z.num = x.num*y.den - y.num*x.den;
- z.reduce(false);
- return z;
- }
- #endif
- }
sffffsub.cpp : last modifiled at 2017/10/20 10:38:06(1,464 bytes)
created at 2015/12/22 16:07:29
The creation time of this html file is 2017/10/21 15:10:35 (Sat Oct 21 15:10:35 2017).